import requests  # 导入requests包
from bs4 import BeautifulSoup
from tkinter import *

url = 'https://www.shanghairanking.cn/rankings/bcur/2020'
strhtml = requests.get(url)
strhtml.encoding = 'UTF-8'
soup = BeautifulSoup(strhtml.text, 'lxml')
allschoolData = []

# 存储学校排名
data = soup.select('#content-box > div.rk-table-box > table > tbody > tr> td:nth-child(1)')
for item in data:
    oneschoolData = {'rank': 'null', 'name': 'null', 'province': 'null', 'type': 'null', 'score': 'null'}
    oneschoolData['rank'] = item.get_text()
    allschoolData.append(oneschoolData)

# 存储学校名称
data = soup.select('#content-box > div.rk-table-box > table > tbody > tr > td> a')
i = 0
for item in data:
    allschoolData[i]['name'] = item.get_text()
    i = i + 1

# 存储学校所在省市
data = soup.select('#content-box > div.rk-table-box > table > tbody > tr > td:nth-child(3)')
i = 0
for item in data:
    allschoolData[i]['province'] = item.get_text()
    i = i + 1

# 存储学校类型
data = soup.select('#content-box > div.rk-table-box > table > tbody > tr> td:nth-child(4)')
i = 0
for item in data:
    allschoolData[i]['type'] = item.get_text()
    i = i + 1

# 存储学校总分
data = soup.select('#content-box > div.rk-table-box > table > tbody > tr > td:nth-child(6)')
i = 0
for item in data:
    allschoolData[i]['score'] = item.get_text()
    i = i + 1

# 去除换行和空格
for i in range(len(allschoolData)):
    allschoolData[i]['rank'] = allschoolData[i]['rank'].strip()
    allschoolData[i]['name'] = allschoolData[i]['name'].strip()
    allschoolData[i]['province'] = allschoolData[i]['province'].strip()
    allschoolData[i]['type'] = allschoolData[i]['type'].strip()
    allschoolData[i]['score'] = allschoolData[i]['score'].strip()


# def printResult(data):
#     print("{:<4}{:<12}{:<8}{:<8}{:<10}".format("排名", "学校名称", "省市", "类型", "总分"))
#     for i in range(len(data)):
#         '''打印什么数据：你得查看具体的网页'''
#         print("{:<4}{:<12}{:<8}{:<8}{:<10}".format(data[i]['rank'], data[i]['name'], data[i]['province'],
#                                                    data[i]['type'], data[i]['score']))


# printResult(allschoolData)

# 查找大学相关信息
def search():
    str = inp1.get()
    sign = 0
    for schoolData in allschoolData:
        if schoolData['name'] == str:
            txt.insert(END,"名称："+schoolData['name']+'\n')
            txt.insert(END, "排名：" + schoolData['rank']+'\t'*2)
            txt.insert(END, "省市：" + schoolData['province']+'\t\t')
            txt.insert(END, "类型：" + schoolData['type']+'\t'*2)
            txt.insert(END, "总分：" + schoolData['score']+'\n')
            sign = 1
    if sign == 0:
        txt.insert(END,"名称："+str+'\n')
        txt.insert(END, "未找到该学校！\n")


root = Tk()
root.title('中国大学排名')
root.geometry('440x440')  # 这里的乘号不是 * ，而是小写英文字母 x
la = Label(root, text="输入你想查询的大学", relief=GROOVE)
la.pack()
inp1 = Entry(root)
inp1.place(relx=0.35, rely=0.2, relwidth=0.3, relheight=0.1)
txt = Text(root)
txt.place(rely=0.6, relheight=0.4)
btn1 = Button(root, text='确定', command=search)
btn1.place(relx=0.35, rely=0.4, relwidth=0.3, relheight=0.1)
root.mainloop()
